package com.panfeng.xcloud.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;

@Component
@Slf4j
public class ImageMarkLogoUtil {

    /**
     *
     * @param srcImgPath
     * @param degree
     * @return
     */
    public byte[] getMarkLogoImage(String srcImgPath, Integer degree){
        String iconUrl = this.getClass().getClassLoader().getResource("logo.png").getPath();
        return getMarkLogoImage(srcImgPath, degree, iconUrl);
    }

    /**
     * 给图片添加水印图片、可设置水印图片的旋转角度
     *
     * @param srcImgPath 源图片路径
     * @param degree     旋转角度
     * @param iconUrl    水印
     */
    public byte[] getMarkLogoImage(String srcImgPath, Integer degree, String iconUrl) {
        ByteArrayOutputStream os = null;
        byte[] bytes = null;
        try {
            // 1、源图片
            Image srcImg = ImageIO.read(new URL(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            // 4、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree), buffImg.getWidth() / 2, buffImg.getHeight() / 2);
            }
            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            //String iconUrl = this.getClass().getClassLoader().getResource("logo.png").getPath();
            log.info(">>> 水印照的logo图片位置：{} <<<", iconUrl);
            ImageIcon imgIcon = new ImageIcon(iconUrl);
            // 得到Image对象。
            Image img = imgIcon.getImage();
            // 7、设置水印透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
            // 8、第一参数->设置水印图片，后面两个参数->水印在图片上的坐标位置(x,y)
            int x = buffImg.getWidth() / 8;
            int y = buffImg.getHeight() / 8;
            x = x < 0 ? 1 : x;
            y = y < 0 ? 1 : y;
            log.info(">>> 水印照的在原图的坐标位置x={},y={}<<<", x, y);
            int width = img.getWidth(null) ;
            int height = img.getHeight(null);
            log.info(">>> 水印照的在原图的绘制的宽高width={},height={}<<<", width, height);
            //g.drawImage(img, x, y, width, height, null);
            //g.drawImage(img, x, y, null);
            g.drawImage(img,(srcImg.getWidth(null) / 7 ) * 2 , (srcImg.getHeight(null) / 5) * 2,img.getWidth(null),img.getHeight(null),null);
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            os = new ByteArrayOutputStream();
            ImageIO.write(buffImg, "jpg", os);
            bytes = os.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.flush();
                    os.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return bytes;
    }


    public static void doMarkLogoImageToStream(String srcImgPath, Integer degree,OutputStream output) {
        try {
            // 1、源图片
            Image srcImg = ImageIO.read(new URL(srcImgPath));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
            // 4、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree), buffImg.getWidth() / 2, buffImg.getHeight() / 2);
            }
            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            String iconUrl = "D:\\Programes\\developerProgrames\\IdeaMyWorkSpacesProjects-artcloud\\artcloud-master\\artcloud-apps\\artcloud-app-boss\\artcloud-app-boss-provider-user\\src\\main\\resources\\logo.png";
            log.info(">>> 水印照的logo图片位置：{} <<<", iconUrl);
            ImageIcon imgIcon = new ImageIcon(iconUrl);
            // 得到Image对象。
            Image img = imgIcon.getImage();
            // 7、设置水印透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
            // 8、第一参数->设置水印图片，后面两个参数->水印在图片上的坐标位置(x,y)
            int x = buffImg.getWidth() / 8;
            int y = buffImg.getHeight() / 8;
            x = x < 0 ? 1 : x;
            y = y < 0 ? 1 : y;
            log.info(">>> 水印照的在原图的坐标位置x={},y={}<<<", x, y);
            int width = img.getWidth(null);
            int height = img.getHeight(null);
            log.info(">>> 水印照的在原图的绘制的宽高width={},height={}<<<", width, height);
            //g.drawImage(img, x, y, width, height, null);
            g.drawImage(img,(srcImg.getWidth(null) / 7 ) * 2 , (srcImg.getHeight(null) / 5) * 2,(img.getWidth(null)/6)*5,(img.getHeight(null)/6)*5,null);
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            ImageIO.write(buffImg, "jpg", output);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != output) {
                    output.flush();
                    output.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            FileOutputStream targerImgOut = new FileOutputStream("d:\\test2.jpg");
            doMarkLogoImageToStream("http://image.weijianapp.com/artcloud-app-mgr-provider-user/20190427/5734f5c4-0dde-4ee1-9c83-70e32716478f.png", 0,targerImgOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
